<?php
	namespace plugins\word\controller;

	use cmf\controller\PluginBaseController;
	use think\Db;
	use tree\Tree;
	use app\common\lib\AccessAuthorization;
	

class PublicController extends PluginBaseController{
	protected function _initalize(){
		parent::_initalize();
		$adminId = cmf_et_current_admin_id();
		if(!empty($adminId)){
			$this->assign("admin_id",$adminId);
		}
	}

	/**
	 * 任务管理模块 菜单注解
	 * @adminMenu(
	 * 		'name'		=>	'文档管理',
	 * 		'parent'	=>	'admin/Plugin/default',
	 * 		'display'	=>	true,
	 * 		'hasView'	=>	true,
	 * 		'order'		=>	9999,
	 * 		'icon'		=>	'',
	 * 		'remark'	=>	'文档管理',
	 * 		'param'		=>	''
	 * )
	 */
	public function index(){
		$param = $this->request->get();
		if(!empty($param['id']) && $param [ 'id'] >= 74 ){
			$id = $param['id'];
		}else{
			$id = 74;
		}
		$this->assign('id',$id);


		/*tree设置*/
		$tree = new Tree();
		$tree->icon = ['&nbsp;&nbsp;&nbsp;&nbsp│', '&nbsp;&nbsp;&nbsp;&nbsp;├', '&nbsp;&nbsp;&nbsp;&nbsp;└'];
		$list = $this->getList( 1 );
		foreach ($list as $key => &$value) {
			if($value['type'] == '1'){
				$value['icon'] = "📁";
			}else{
				$value['disabled'] = "";
				$value['icon'] = "📄";
			}
			
			if($value['id'] == $id){
				$value['selected'] = "selected='selected'";
			}

		}
		$str = "<option value=\$id \$selected>\$spacer \$icon \$name</option>";
		/*tree设置*/

		//内容放入
		$tree->init($list);
		//tree生成
		$select =  $tree->getTree('74',$str);

		$this->assign('select',$select);
		return $this->fetch( 'admin_index/public');
	}
	
	//得到列表,
    //type1为file 2为word,有这个参数则搜索指定类型
    //pid有的情况下搜索指定id下的子元素
    private function getList($uid,$type=0,$pid=null){
    	$where = '';

    	$where .= '(';
    	//公有
    	$where .= 'private = 0';

    	//自身创建的
    	//$where .= ' or create_user = '.$uid;
    	
    	//私有
    	$private = db('word_private')->field("word_id")->where(['reader_id'=>$uid])->select()->toArray();
		if($private){
			foreach ($private as $key => $value) {
				$where .= " or id = ".$value['word_id'];
			}
		}

		/*//权限
    	$limit = new AccessAuthorization();
    	$access = $limit->getAccessibleIdsByUserId($uid);
    	foreach ($access as $key => $value) {
    		$where .= " or create_user = ".$value;
    	}
*/

		$where .= ')';

		$where .= " and status = 0";

		//指定文件类型时
    	if($type != 0){
    		$where .= " and type = ".$type;
    	}

    	//指定id文件下的文件
    	if(!is_null($pid)){
    		$where .= " and parent_id = ".$pid;
    	}
    	//搜索对应条件的结果
    	$list = db('word')->field("id,type,parent_id,name")->where($where)->order('sort')->select()->toArray();
    	return $list;
    }
	
	
	//得到对应的文章内容
	public function wordContent(){
		$param = $this->request->get();
		//权限判断用
		$uid = cmf_get_current_admin_id();
		$info = '';
		//主页的搜索
		if($param['id'] == 0){
			$parents = $this->getList($uid,0,$param['id']);
			$info['content'] = '';
				//要显示的内容
			foreach ($parents as $key => $value) {
				$info['content'] .= "<li class='layui-timeline-item' value='".$value['id']."' ondblclick='tz(".$value['id'].")'>";
				$info['content'] .= "<div class='layui-timeline-content layui-text'>";
				$info['content'] .= "<a>";
				if($value['type'] == '1'){
					$info['content'] .= "📁";
				}else{
					$info['content'] .= "📄";
				}
				$info['content'] .= $value['name'];
				$info['content'] .= "</a>";
				//权限检测
				$return = $this->wordCheck($uid,$value['id']);
				
				$info['content'] .= "</div>";
				$info['content'] .= "</li>";
			}	

			$info['content'] .= "<script type='text/javascript'>sort();</script>";	

			return json(['type'=>'success','msg'=>'查询成功','info'=>$info]);	
		}

		$info = db('word')->where(['id'=>$param['id'],'status'=>0])->find();
		if($info){
			//文档创建人名字获取
			$info['uname'] = $this->uidGetName($info['create_user']);

			$return = $this->wordCheck($uid,$param['id']);
			$info['del'] = $return['del'];
			$info['edit'] = $return['edit'];
			// //编辑权限判断
			// if($info['create_user'] == $uid){
			// 	$info['edit'] = 1;
			// }else{
			// 	$info['edit'] = 0;
			// }

			// //自身以下的权限
	  //   	$limit = new AccessAuthorization();
	  //   	//当前用户下属
	  //   	$access = $limit->getAccessibleIdsByUserId($uid);
	  //   	$search = array_search($info['create_user'],$access);
	  //   	if($search !== false){
	  //   		$info['edit'] = 1;
	  //   	}

	  //   	if($this->delCheck($info['id'])){
	  //   		//有子级，不可删除
	  //   		$info['del'] = 0;
	  //   	}else{
	  //   		//无子级，可以删除
	  //   		$info['del'] = 1;
	  //   	}

			//选中文件是文件夹的情况
			if($info['type'] == 1){
				//得到子文件
				$parents = $this->getList($uid,0,$info['id']);
				$info['content'] = '';
				// $info['content'] .= "<ul id='sortable'>";
				//要显示的内容
				foreach ($parents as $key => $value) {
					$info['content'] .= "<li class='layui-timeline-item' value='".$value['id']."' ondblclick='tz(".$value['id'].")'>";
					$info['content'] .= "<div class='layui-timeline-content layui-text'>";
					$info['content'] .= "<a>";
					if($value['type'] == '1'){
						$info['content'] .= "📁";
					}else{
						$info['content'] .= "📄";
					}
					$info['content'] .= $value['name'];
					$info['content'] .= "</a>";
					//权限检测
					$return = $this->wordCheck($uid,$value['id']);
					
					$info['content'] .= "</div>";
					$info['content'] .= "</li>";

					$info['content'] .= "<script type='text/javascript'>sort();</script>";	

				// $info['content'] .= "</ul>";
				}
			}else{
				//文本化
				$info['content'] = html_entity_decode($info['content']);
			}
			if( $info [ 'id'] <= 74 ){
				$info ['parent_id'] = 74;
				
			}
			return json(['type'=>'success','msg'=>'查询成功','info'=>$info]);
		}else{
			return json(['type'=>'error','msg'=>'查询出错']);
		}	
	}
	
	
	//用户id获得用户名
    private function uidGetName($uid){
    	$info = db('user')->field('user_login')->where(['id'=>$uid])->find();
    	$uname = $info['user_login'];
    	if($uname){
    		return $uname;
    	}
    	return '';
    }
	
	
	//传入用户id与文件id查询权限
    private function wordCheck($uid,$id){
    	//搜索创建人
    	$find = db('word')->field("create_user")->where(['id'=>$id])->find();
    	$return = [];
		//通过创建人及你想编辑权限判断
		if($find['create_user'] == $uid){
			$return['edit'] = 1;
		}else{
			$return['edit'] = 0;
		}

		//自身以下的权限
    	$limit = new AccessAuthorization();
    	//当前用户下属
    	$access = $limit->getAccessibleIdsByUserId($uid);
    	$search = array_search($find['create_user'],$access);
    	if($search !== false){
    		$return['edit'] = 1;
    	}

    	//删除的检测
    	//没有编辑权限也没有删除权限
    	if($return['edit'] == 0 ){
    		$return['del'] = 0;
    		return $return;
    	}

    	$del = db('word')->field('id')->where(['parent_id'=>$id,'status'=>0])->select()->toArray();
    	if($del){
    		//有子级,不可删除
    		$return['del'] = 0;
    	}else{
    		//无子级.可删除
    		$return['del'] = 1;
    	}

    	return $return;
    }
	
	
	
	
	
	
	
}